--
-- Author: shunguo.chen
-- Date: 2016-08-09 13:53:57
--
-- PayRender

local ClassRef = app.mvc.createRenderClass(...)

local PRICE_STR = L("demondShop_rmb")

local DIAMOND_STR = L("lua_code_text_184")

--data = PayVo
function ClassRef.create(node)
	local sprite = ClassRef.extend(node or cc.CSLoader:createNode(Res.PayNode))
	if not node then
		ccui.Helper:doLayout(sprite)
	end
	sprite:init()
	return sprite
end

function ClassRef:init(data)
	self.super.init(self, data)

	self._ui.btn_buy:addClickEventListener(function()
		if self._data then
			executeFunc(self._onBtnClick, self._data)
		end
	end)

	self._priceIcon = self._ui.price_1:getChildByName("icon")
	self._priceText = self._ui.price_1:getChildByName("num")

	self._extraDiamondText = self._ui.tips_give:getChildByName("num")

	if app:getInnerPublisher() ~= Enums.Publisher.tencent then
		self._priceIcon:setVisible(false)
	else
		self._priceIcon:setVisible(true)
	end
	self:refresh()
end

function ClassRef:refresh()
	if self._data then
		local data = self._data
		self._ui.Image:loadTexture(data:getIcon())
		-- self._ui.tips_hot:setVisible(data.isHot)

		display.setBtnTouchEnable(self._ui.btn_buy, data:isOpen())

		-- diamond
		local numCon = self._ui["Panel/num"]
		local dot = numCon:getChildByName("gold")
		local numText = numCon:getChildByName("num")
		numText:setString(string.format(DIAMOND_STR, data.diamond))
		display.setNodesMiddle(numCon, dot, numText)

		-- extra diamond
		local extraDiamond = data:isFirst() and data.firstExtraDiamond or data.extraDiamond
		self._ui.tips_give:setVisible(extraDiamond > 0)
		if extraDiamond > 0 then
			self._extraDiamondText:setString(string.format(DIAMOND_STR, extraDiamond))
		end
		-- price
		self._priceText:setString(data.price)
		if app:getInnerPublisher() ~= Enums.Publisher.tencent then
			display.setNodesMiddle(self._ui.price_1, nil, self._priceText)
		else
			display.setNodesMiddle(self._ui.price_1, nil, self._priceIcon, self._priceText)
		end
	end
end

function ClassRef:onBtnClick(cb)
	self._onBtnClick = cb
end


function ClassRef:getRenderWidth()
	return self._ui.Panel:getContentSize().width
end
return ClassRef